package com.zgc.view.common;

import com.jfoenix.animation.alert.JFXAlertAnimation;
import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXTextArea;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.springframework.stereotype.Component;


@Component
public class AlertCtrl {

    private JFXAlert<String> alert;
    private JFXTextArea messageArea;

    private void initialize() {
        if (alert == null) {
            alert = new JFXAlert<>();
            messageArea = new JFXTextArea();
            messageArea.setFont(Font.font(12));
            messageArea.setEditable(false);
            messageArea.setWrapText(true);
            messageArea.setPadding(new Insets(20));
            alert.setContent(messageArea);
            alert.setTitle("提示");
            setIcons("/view/images/icon.png");
            alert.setAnimation(JFXAlertAnimation.BOTTOM_ANIMATION);
        }
    }

    public void setIcons(String iconsPath) {
        Stage window = (Stage) alert.getDialogPane().getScene().getWindow();
        window.getIcons().add(new Image(iconsPath));
    }


    public void setMessageAndShow(String title, String message) {
        setMessageAndShow(message);
        alert.setTitle(title);
    }

    public void setMessageAndShow(String message) {
        initialize();
        messageArea.setText(message);
        show();
    }

    public void show() {
        if (alert != null) {
            alert.show();
        }

    }

    public void close() {
        if (alert != null) {
            alert.close();
        }
    }
}
